package xiegd.study.proxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.stereotype.Component;
import xiegd.study.proxy.annotation.ClassLog;
import xiegd.study.proxy.annotation.MethodLog;

import java.lang.reflect.Method;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2023/9/20 9:44
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Object instance = bean;
        Class<?> aClass = bean.getClass();
        ClassLog classLog = aClass.getAnnotation(ClassLog.class);
        //注解的值为true
        if(classLog!= null && classLog.value()){
            Method[] methods = aClass.getMethods();
            for(Method method : methods){
                MethodLog annotation = method.getAnnotation(MethodLog.class);
                //注解的值为开启
                if(annotation != null && annotation.value().equals("开启")){
                    //如果存在一个，则可以返回了，因为都是代理了同一个；不必须循环了
                    instance = getInstans(aClass);
//                    Field[] declaredFields = aClass.getDeclaredFields();
//                    //List<Field> fieldsByAnnotation = ReflectUtil.getAllFieldsByAnnotation(aClass, Value.class);
//                    for(Field field : declaredFields){
//                        Value tt = field.getAnnotation(Value.class);
//                        if(tt != null){
//                            try {
//                                field.setAccessible(true);
//                                Object o = field.get(bean);
//                                System.out.println("两者的区别是什么:" + bean + ",,," + o);
//                                field.set(instance, "我修改了");
//                            } catch (IllegalAccessException e) {
//                                e.printStackTrace();
//                            }
//                            System.out.println("我是启动的时候看到的:" + tt.value() + ",:::" + field.getName());
//                        }
//                    }

                    break;
                }
            }
        }


        return instance;
    }

    private Object getInstans(Class<?> aClass) {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(aClass);
//        enhancer.setCallback(new MethodInterceptor() {
//            @Override
//            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//                MethodLog annotation = method.getAnnotation(MethodLog.class);
//                //System.out.println("我是拦截器之前的" + method);
//                //注解的值为开启
//                if(annotation != null && annotation.value().equals("开启")){
//                    System.out.println("我是拦截器之前的" + method);
//                }
//                Object o1 = methodProxy.invokeSuper(o, objects);
//                if(annotation != null && annotation.value().equals("开启")){
//                    System.out.println("我是拦截器之后的"+ method);
//                }
//                //System.out.println("我是拦截器之后的"+ method);
//                return o1;
//            }
//        });
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
                    MethodLog annotation = method.getAnnotation(MethodLog.class);
                    //System.out.println("我是拦截器之前的" + method);
                    //注解的值为开启
                    if (annotation != null && annotation.value().equals("开启")) {
                        System.out.println("我是拦截器之前的" + method);
                    }
                    Object o1 = methodProxy.invokeSuper(o, objects);
                    if (annotation != null && annotation.value().equals("开启")) {
                        System.out.println("我是拦截器之后的" + method);
                    }
                    return o1;
                }
        );

        return enhancer.create();
    }
}
